package com.micromall.utils;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

	public static final  String                                      YYYY_MM_DD          = "yyyy-MM-dd";
	public static final  String                                      HH_MM_SS            = "HH:mm:ss";
	public static final  String                                      YYYY_MM_DD_HH_MM    = "yyyy-MM-dd HH-mm";
	public static final  String                                      YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static final  String                                      YYYYMMDD            = "yyyyMMdd";
	public static final  String                                      HHMMSS              = "HHmmss";
	public static final  String                                      YYYYMMDDHHMM        = "yyyyMMddHHmm";
	public static final  String                                      YYYYMMDDHHMMSS      = "yyyyMMddHHmmss";
	public static final  String                                      DEFAULT_MONTH       = "MONTH";
	public static final  String                                      DEFAULT_YEAR        = "YEAR";
	public static final  String                                      DEFAULT_DATE        = "DAY";
	private static final Logger                                      logger              = LoggerFactory.getLogger(DateUtils.class);
	private static       ConcurrentHashMap<String, SimpleDateFormat> dataFormats         = new ConcurrentHashMap<String, SimpleDateFormat>();

	private static SimpleDateFormat _DataFormat(String format) {
		/*if (!dataFormats.containsKey(format.intern())) {
			dataFormats.putIfAbsent(format.intern(), new SimpleDateFormat(format));
		}
		return dataFormats.get(format.intern());*/
		return new SimpleDateFormat(format);
	}

	public static String format(Date date, String format) {
		return _DataFormat(format).format(date);
	}

	public static Date parseDate(String date, String format) {
		if (date == null) {
			logger.warn("转换日期失败：format：{}，date：{}", format, date);
			return null;
		}
		try {

			return _DataFormat(format).parse(date);
		} catch (Exception e) {
			logger.error("转换日期出错：format：{}，date：{}，error：", format, date, e);
		}
		return null;
	}

	public static Date cleanHHMMSS(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	private static List<Date> getDates(Date start, Date end) {
		Calendar startCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		Calendar temp = Calendar.getInstance();
		startCalendar.setTime(start);
		endCalendar.setTime(end);
		temp.setTime(start);

		List<Date> result = new ArrayList<>();
		while (temp.before(endCalendar)) {
			result.add(temp.getTime());
			temp.add(Calendar.DATE, 1);
		}
		result.add(end);
		return result;
	}

	public static void main(String[] args) {
		Date current = DateUtils.cleanHHMMSS(new Date());
		Date startDate = DateUtils.addMonths(current, -1);
		Date endDate = current;

		// 生成开始/截止时间之间每隔时间点的数据（默认数据都为0）
		Map<String, Object> _map = new LinkedHashMap<>();
		List<Date> datelist = getDates(startDate, endDate);
		for (Date date : datelist) {
			String _date = DateUtils.format(date, DateUtils.YYYY_MM_DD);
			_map.put(_date, 0);
		}
		System.out.println(JSON.toJSONString(_map));
	}
}